我正在尝试创建一个If语句,允许我将数字和值发送到另一个工作簿,加深第三个单元格中的值(天气单元格是真还是假“)。我要运行的代码如果语句为True则在下面。我已经测试了这段代码,并且它工作正常,我只是想让它在行中的第三个单元格为True时发生。然后它需要携带数值和值行中的信息值对于下面的函数是正确的。
Dim itemName As String
Dim itemPrice As String
Dim myData As Workbook
Worksheets("Sheet1").Select
InvoiceNumber = Range("A2")
qty = Range("B2")
updated = Range("C2")
Set myData = Workbooks.Open("HD:Users:user:Desktop:Inventory.xlsm")
Worksheets("Orders").Select
Worksheets("Orders").Range("a15").Select
RowCount = Worksheets("Orders").Range("a15").CurrentRegion.Rows.Count
With Worksheets("Orders").Range("a15")
.Offset(RowCount, 0) = InvoiceNumber
.Offset(RowCount, 1) = qty
.Offset(RowCount, 2) = updated
End With
答案 0 :(得分:1)
您的代码看起来可能会使用另一个循环来浏览一系列行,但我想这可能只是需要单个操作的情况。
Dim itemName As String
Dim itemPrice As String
Dim myData As Workbook
Dim sInvoiceNumber As String, dQty As Double, bUpdated As Boolean, iRowCount As Long
With Worksheets("Sheet1")
sInvoiceNumber = .Range("A2").Value
dQty = .Range("B2").Value
bUpdated = CBool(.Range("C2").Value)
If bUpdated Then
Set myData = Workbooks.Open("HD:Users:user:Desktop:Inventory.xlsm")
With myData.Worksheets("Orders").Range("a15")
iRowCount = .CurrentRegion.Rows.Count
.Offset(iRowCount, 0) = sInvoiceNumber
.Offset(iRowCount, 1) = dQty
.Offset(iRowCount, 2) = bUpdated
End With
myData.Close True '<-save and close the workbook
.Range("C2") = Not bUpdated '<- reset the updated field
End If
End With
不是循环遍历行,而是可以通过过滤数据来使用批量传输操作。
Dim itemName As String
Dim itemPrice As String
Dim myData As Workbook
Dim iRowCount As Long
With Worksheets("Sheet1")
If .AutoFilterMode Then AutoFilterMode = False
With .Cells(1, 1).CurrentRegion
.AutoFilter Field:=3, Criteria1:="TRUE"
With .Offset(1, 0).Resize(.Rows.Count - 1, 3)
If CBool(Application.Subtotal(103, .Columns(1))) Then
Set myData = Workbooks.Open("HD:Users:user:Desktop:Inventory.xlsm")
.Cells.Copy Destination:=myData.Sheets("Orders").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
myData.Close True '<-save and close the workbook
.Columns(3).Cells = False
End If
End With
.AutoFilter Field:=3
End With
If .AutoFilterMode Then AutoFilterMode = False
End With
在C列中过滤工作表单元格值TRUE。
答案 1 :(得分:0)
你应该阅读if语句。我猜你能从这里弄明白:
http://www.techonthenet.com/excel/formulas/if_then.php
基本上,只需在if语句中使用updated作为测试条件。
Dim itemName As String
Dim itemPrice As String
Dim myData As Workbook
Worksheets("Sheet1").Select
InvoiceNumber = Range("A2")
qty = Range("B2")
updated = Range("C2")
If updated Then
Set myData = Workbooks.Open("HD:Users:user:Desktop:Inventory.xlsm")
Worksheets("Orders").Select
Worksheets("Orders").Range("a15").Select
RowCount = Worksheets("Orders").Range("a15").CurrentRegion.Rows.Count
With Worksheets("Orders").Range("a15")
.Offset(RowCount, 0) = InvoiceNumber
.Offset(RowCount, 1) = qty
.Offset(RowCount, 2) = updated
End With
End If