VB.net 将一列从一个 sheet 复制并粘贴到另一个 sheet
VB.net copying and pasting a column from one sheet to another sheet
我一直在努力打开现有的工作簿,同时在该工作簿中创建两个新的 sheet。完成后,我试图将工作簿中现有 sheet 的整个列复制到我创建的新 sheet 之一,然后删除该列中的重复项以及空行。我已经创建了两个新的 sheet,但我的副本似乎不起作用,该列是空的。我在删除空行时也有错误。所有见解都会有所帮助!
'Open up the workbook and create two need worksheets (Unique Numbers & Attributes).
If IO.File.Exists("C:\Users\file.xlsm") Then xlWorkBook = xlApp.Workbooks.Open("C:\Users\file.xlsm")
Dim worksheets As Excel.Sheets = xlWorkBook.Worksheets
Dim xlNewSheet = DirectCast(worksheets.Add(worksheets(1), Type.Missing, Type.Missing, Type.Missing), Excel.Worksheet)
xlNewSheet.Name = "Unique Numbers"
Dim xlNewSheet2 = DirectCast(worksheets.Add(worksheets(1), Type.Missing, Type.Missing, Type.Missing), Excel.Worksheet)
xlNewSheet2.Name = "Attributes"
'Populate the Unique Numbers worksheet.
Dim trace As Object
Dim unique_numbers As Object
trace = xlWorkBook.Sheets("2020-12-16_12-07-12_781") 'The existing worksheet in the workbook
unique_numbers = xlWorkBook.Sheets("Unique Numbers")
'Extract the values from trace and copy them to unique numbers, then remove duplicates and empty rows
trace.Range("E:E").Copy 'I can confirm this row has data
unique_numbers.Range("A:A").PasteSpecial(Excel.XlPasteType.xlPasteFormats)
unique_pgns.Range("A:A").RemoveDuplicates()
'Loop through range ("A")
Dim rowCount As Integer = unique_numbers.Rows.Count()
Dim r As Integer
For r = rowCount - 1 To 0 Step -1
If unique_numbers.Rows(r) Is "" Then
unique_numbers.DeleteRow(r + 1)
End If
Next
经过多次尝试,终于弄明白了。
trace.Range("E:E").Copy()
unique_numbers.Range("A:A").PasteSpecial(Excel.XlPasteType.xlPasteAll)
unique_numbers.Range("A:A").RemoveDuplicates(Columns:=1,Header:=Excel.XlYesNoGuess.xlYes)
我一直在努力打开现有的工作簿,同时在该工作簿中创建两个新的 sheet。完成后,我试图将工作簿中现有 sheet 的整个列复制到我创建的新 sheet 之一,然后删除该列中的重复项以及空行。我已经创建了两个新的 sheet,但我的副本似乎不起作用,该列是空的。我在删除空行时也有错误。所有见解都会有所帮助!
'Open up the workbook and create two need worksheets (Unique Numbers & Attributes).
If IO.File.Exists("C:\Users\file.xlsm") Then xlWorkBook = xlApp.Workbooks.Open("C:\Users\file.xlsm")
Dim worksheets As Excel.Sheets = xlWorkBook.Worksheets
Dim xlNewSheet = DirectCast(worksheets.Add(worksheets(1), Type.Missing, Type.Missing, Type.Missing), Excel.Worksheet)
xlNewSheet.Name = "Unique Numbers"
Dim xlNewSheet2 = DirectCast(worksheets.Add(worksheets(1), Type.Missing, Type.Missing, Type.Missing), Excel.Worksheet)
xlNewSheet2.Name = "Attributes"
'Populate the Unique Numbers worksheet.
Dim trace As Object
Dim unique_numbers As Object
trace = xlWorkBook.Sheets("2020-12-16_12-07-12_781") 'The existing worksheet in the workbook
unique_numbers = xlWorkBook.Sheets("Unique Numbers")
'Extract the values from trace and copy them to unique numbers, then remove duplicates and empty rows
trace.Range("E:E").Copy 'I can confirm this row has data
unique_numbers.Range("A:A").PasteSpecial(Excel.XlPasteType.xlPasteFormats)
unique_pgns.Range("A:A").RemoveDuplicates()
'Loop through range ("A")
Dim rowCount As Integer = unique_numbers.Rows.Count()
Dim r As Integer
For r = rowCount - 1 To 0 Step -1
If unique_numbers.Rows(r) Is "" Then
unique_numbers.DeleteRow(r + 1)
End If
Next
经过多次尝试,终于弄明白了。
trace.Range("E:E").Copy()
unique_numbers.Range("A:A").PasteSpecial(Excel.XlPasteType.xlPasteAll)
unique_numbers.Range("A:A").RemoveDuplicates(Columns:=1,Header:=Excel.XlYesNoGuess.xlYes)