既然你是新的编码VBA,我用For和If做了一个简单的代码,试着学习这个函数。 我没有声明每个变量,尝试声明它们并了解数据类型。 LAST_ROW LAST_COLUMN和是变量,可以帮助很多在Excel VBA中,主要是为了使动态电子表格
Sub test()
'Declare variables here
Dim sht1, sht2 As Worksheet
'sht1 has the data and sht2 is the output Worksheet, you can change the names
Set sht1 = ThisWorkbook.Worksheets("Sheet1")
Set sht2 = ThisWorkbook.Worksheets("Sheet2")
last_row1 = sht1.Range("A65536").End(xlUp).Row
last_column = sht1.Cells(1, sht1.Columns.Count).End(xlToLeft).Column
x = 1
'Add header
sht2.Cells(1, 1) = "Company"
sht2.Cells(1, 2) = "Country"
sht2.Cells(1, 3) = "Status"
'Data add
For i = 2 To last_row1
'Assuming Company is in Column 1 or A
For k = 2 To last_column
'If cell is different from blank then write data
If sht1.Cells(i, k) <> "" Then
sht2.Cells(x + 1, 1) = sht1.Cells(i, 1)
sht2.Cells(x + 1, 2) = sht1.Cells(1, k)
sht2.Cells(x + 1, 3) = sht1.Cells(i, k)
x = x + 1
End If
Next k
Next i
MsgBox "Data Updated"
End Sub
下一次,尝试学习一些编码和发布您的代码。
我通常会自己做一些工作,但在这种情况下,我真的很努力地找到任何接近我想要实现的东西。下次我会更努力。这工作非常好,非常感谢。我已标记为正确的答案。 –