返回列表 上一主題 發帖

[發問] 關於VBA 用於VB按鈕問題。

[發問] 關於VBA 用於VB按鈕問題。

想請教大家如何將VBA的源碼搬進VB的按鈕內,當我按下VB的按鈕後他會執行相關的
動作,想請教以下兩大段VBA 源碼應如何修改至VB內,感謝。

Sub Job_No()

Dim a As Integer

Dim b As Integer

Dim rowNum As Integer

Dim Book As Integer

Dim BookName As String





BookNum = Workbooks.Count


'MsgBox BookNum


For a = 1 To BookNum

BookName = Workbooks(a).Name

Workbooks(BookName).Activate



Range("V:AE").Select

Selection.Delete Shift:=xlToLeft


x = 0
Range("b5").Select

Do
If ActiveCell.Value <> "" Then
x = 0
ActiveCell.Offset(1, 0).Select
Else
Selection.EntireRow.Delete
x = x + 1
End If
Loop Until x > 300
Range("A1").Select


Range("U4") = "#"

Range("U5").Formula = "=LEFT(G1,5)&U4&MID(G1,6,6)"

Range("U5").Select

Selection.Copy

Range("U5").PasteSpecial xlPasteValuesAndNumberFormats


[U5].Copy


With Range("U6:U" & [A65536].End(xlUp).Row)
    .PasteSpecial xlPasteFormulas
    .Value = .Value



End With

Next a
End Sub

Sub unite_data()

Dim BookNum As Integer, rowNum As Integer, currentRow As Integer
Dim i As Integer
Dim j As Integer
Dim BookName As String



currentRow = 2
BookNum = Workbooks.Count
'MsgBox BookNum

For i = 1 To BookNum
BookName = Workbooks(i).Name
If BookName <> "Data.xlsx" Then

     For j = 1 To 1
      Workbooks(BookName).Activate
      
      rowNum = Workbooks(BookName).Worksheets("result").UsedRange.Rows.Count
      'MsgBox rowNum
         
      
      If (rowNum - 1 >= 1 And Workbooks(BookName).Worksheets("result").Cells(5, 1).Value <> "") Then
        Workbooks(BookName).Worksheets("result").Rows("5:" & rowNum).Select
        Selection.Copy
        Windows("Data.xlsx").Activate
        Rows(currentRow).Select
        ActiveSheet.Paste
        currentRow = currentRow + (rowNum - 4)
        Windows(BookName).Activate
      End If
     Next j

End If
   
Next i



Windows("Data.xlsx").Activate

Range("V2:V500").Value = "1"


Range("W2").Formula = "=P2"

[W2].Copy

With Range("W3:W" & [A65536].End(xlUp).Row).PasteSpecial



Range("X2").Formula = "=P2"

[X2].Copy

With Range("X3:X" & [A65536].End(xlUp).Row).PasteSpecial


Range("Y2").Formula = "=U2"

[Y2].Copy

With Range("Y3:Y" & [A65536].End(xlUp).Row).PasteSpecial

Range("V2:V500").Select

With Selection.font

.Size = 15

Range("X2:X500").Select

With Selection.font

.Size = 15




Range("V2:V500").Select

With Selection.font

.Size = 15


Range("W2:W500").Select

With Selection.font

.Size = 15


Range("Y2:Y500").Select

With Selection.font

.Size = 15





End With

End With

End With

End With


End With

End With

End With

End With

End Sub

回復 1# stephenlee


    大致上並無差異,
Ranger我會用Cells來寫,
VB.net去算Excel的儲存格位置,
在位置上的變化性高又簡單計算。

不能執行的部分應該是End(xlUp)
在VB.net中要在Form特別宣告Excel計算值,
只要在最上面加入這個就跟VBA算法一樣了。
  1. Const xlUp = -4162
  2. Const xlDown = -4121
複製代碼
一分努力,一分收穫。
發問題前可以先搜索內文是否有相關範例。

TOP

        靜思自在 : 【蒙蔽的自由】人常在什麼都可以自由自在的時候,卻被這種隨心所欲的自由蒙蔽,虛擲時光而毫無覺知。
返回列表 上一主題