[英]MS Excel VBA - Looping through columns and rows
Hello stackoverflow community, 您好stackoverflow社区,
I must confess I primarily code within MS Access and have very limited experience of MS Excel VBA. 我必须承认我主要是在MS Access中编写代码,并且对MS Excel VBA的经验非常有限。
My current objective is this, I have an "Expense Report" being sent to me with deductions, this report has many columns with different account names that may be populated or may be null. 我当前的目标是,我收到一个带有扣除额的“费用报告”,该报告有许多列,其中包含不同的帐户名,这些列可能填充或为空。
My first step will be to start on the first record (Row 14; Column AK contains personal info regarding the deduction) then skip to the first deduction account (deduction accounts start at column L and span to column DG) checking if each cell is null, if it is then keep moving right,If there is a value present, I need to copy it into an external workbook "Payroll Template" starting at row 2 (Column J for the deduction itself), as well as copy some personal info from the original row in the "Expense Report" related to that deduction (currRow: Column C,E,F from "Expense Report" to "Payroll Template" Columns B,C,D). 我的第一步将是从第一个记录开始(行14;列AK包含有关扣减的个人信息),然后跳到第一个扣减帐户(扣减帐户从L列开始到DG列),检查每个单元格是否为空,然后继续向右移动。如果有值存在,我需要将其复制到第二行(扣除项本身为J列)开始的外部工作簿“工资单模板”中,并从中复制一些个人信息与此扣减相关的“费用报告”中的原始行(currRow:从“费用报告”到“工资单模板”列B,C,D的C,E,F列)。
Then move to the right until the next cell contains a value, and repeat this process on a new row in the "Payroll Template". 然后向右移动,直到下一个单元格包含一个值,然后对“工资单模板”中的新行重复此过程。 Once the last column (DG) has been executed I want to move to the next row (row 15) and start the process again all the way until the "LastRow" in my "Used Range".
一旦执行了最后一列(DG),我想移至下一行(第15行),并再次开始该过程,直到“使用范围”中的“ LastRow”。
I greatly appreciate any feedback, explanations, or links that may point me towards my goal. 我非常感谢任何可能将我引向目标的反馈,解释或链接。 Thank you in advance for taking the time to read though this!
预先感谢您抽出宝贵的时间阅读本文!
Current state of code: 代码的当前状态:
`< Sub LoadIntoPayrollTemplate()
Dim rng As Range
Dim currRow As Integer
Dim UsedRng As Range
Dim LastRow As Long
Set UsedRng = ActiveSheet.UsedRange
currRow = 14
Set wb = ActiveWorkbook '"Expense Report"
Set wb2 = MyFilepath '"Payroll Template"
'Copied from another procedure, trying to use as reference
LastRow = rng(rng.Cells.Count).Row
Range("A14").Select
Do Until ActiveCell.Row = LastRow + 1
If (ActiveCell.Value) <> prev Then
currRow = currRow + 1
End If
ActiveCell.Offset(1, 0).Select
Loop
With Worksheets("Collections")
lstRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set rng = .Range(.Cells(14, 12), Cells(lstRow, 111))
End With
End Sub>`
The following code may do what you are after: 以下代码可以完成您的工作:
Sub LoadIntoPayrollTemplate()
Dim currRowIn As Long
Dim currColIn As Long
Dim currRowOut As Long
Dim wb As Workbook
Dim wb2 As Workbook
Set wb = ActiveWorkbook '"Expense Report"
Set wb2 = Workbooks.Open(Filename:=MyFilepath & "\" & "Payroll Template.xlsx")
'or perhaps
'Set wb2 = Workbooks.Open(Filename:=wb.path & "\" & "Payroll Template.xlsx")
With wb.ActiveSheet
currRowOut = 1
For currRowIn = 14 To .UsedRange.Row + .UsedRange.Rows.Count - 1
For currColIn = 12 To 111
If Not IsEmpty(.Cells(currRowIn, currColIn)) Then
currRowOut = currRowOut + 1
'I'm not sure which worksheet you want to write the output to
'so I have just written it to the first one in Payroll Template
wb2.Worksheets(1).Cells(currRowOut, "J").Value = .Cells(currRowIn, currColIn).Value
wb2.Worksheets(1).Cells(currRowOut, "B").Value = .Cells(currRowIn, "C").Value
wb2.Worksheets(1).Cells(currRowOut, "C").Value = .Cells(currRowIn, "E").Value
wb2.Worksheets(1).Cells(currRowOut, "D").Value = .Cells(currRowIn, "F").Value
End If
Next
Next
End With
'Save updated Payroll Template
wb2.Save
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.