I am getting a runtime error when running this marco in excel. The code when rows = 1 works perfectly but I get a runtime error after the else. I can't seem to figure out the problem
If rows = 1 Then
Sheets("Sales Report").Range("C" & 1 + rows).Formula = "=SUMIFS('Stock Log'!$I$2:$I$1048576,'Stock Log'!$H$2:$H$1048576,B2,'Stock Log'!$B$2:$B$1048576,""<=""&A2)"
Else
Sheets("Sales Report").Range("C" & 1 + rows).Formula = "=SUMIFS('Stock Log'!$I$2:$I$1048576,'Stock Log'!$H$2:$H$1048576," & Cells(rows + 1, 2).Address & ",'Stock Log'!$B$2:$B$1048576,""<=""" & Cells(rows + 1, 1).Address & ",'Stock Log'!$B$2:$B$1048576,"">""" & Cells(rows, 1).Address & ")"
End If
Related
I am working on SAP Gui Scripting with VBA. I have 200line of information to enter into SAP from excel. However, SAP list hits 23lines max, which you have to than scroll down to enter more lines into the list. Here is my code
i = 0
With session
.findById("wnd[0]").maximize
.findById("wnd[0]/tbar[0]/okcd").Text = "MIGO"
.findById("wnd[0]").sendVKey 0
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_HEADER:SAPLMIGO:0101/subSUB_HEADER:SAPLMIGO:0100/tabsTS_GOHEAD/tabpOK_GOHEAD_GENERAL/ssubSUB_TS_GOHEAD_GENERAL:SAPLMIGO:0112/txtGOHEAD-BKTXT").Text = "PKG QTY REF 015835"
While Cells(7 + i, 1).Value <> ""
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-MAKTX[1," & i & "]").Text = Cells(7 + i, 2)
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/txtGOITEM-ERFMG[4," & i & "]").Text = Cells(7 + i, 4)
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-ERFME[5," & i & "]").Text = "PC"
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-LGOBE[6," & i & "]").Text = "BORD"
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-NAME1[12," & i & "]").Text = "2S98"
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-UMLGOBE[27," & i & "]").Text = "DMDV"
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-UMBAR[32," & i & "]").Text = "CATNEW"
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-UMBAR[32," & i & "]").SetFocus
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM/ctxtGOITEM-UMBAR[32," & i & "]").caretPosition = 6
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM").verticalScrollbar.Position = i
.findById("wnd[0]").sendVKey 0
i = i + 1
Wend
End With
I tried using
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM").verticalScrollbar.Position = i
to scroll down the list but apparently VBA still stops at line 23. Is there another for all my 200lines to be added inside the list?
Here is a picture of how it looks like when my code ran, There are lines below 23 but vba still stops and doesnt continue. Any help?
Hi Have you tried manually entering data above 23 lines? Are these lines are disabled after 23rd row?
Below should do the job. I usually scroll and set my last row on top. BTW Which t-code are you using?
.findById("wnd[0]/usr/ssubSUB_MAIN_CARRIER:SAPLMIGO:0006/subSUB_ITEMLIST:SAPLMIGO:0200/tblSAPLMIGOTV_GOITEM").verticalScrollbar.Position = i
I've scoured the internet for an answer to my problem. I am writing some code to input a formula into certain cells on a worksheet and despite very similar code working perfectly earlier in the macro, this section of code will not work with giving me the runtime error 1004: application-defined or object-defined error.
I have tried moving my code into a new workbook but the problem was not solved and I just can't see why it won't work.
The code below is where I define the sheets I am using
Sub InputFormulae()
Dim wksht As Worksheet
Dim wksht1 As Worksheet
Dim wksht2 As Worksheet
Dim wksht3 As Worksheet
Dim wksht4 As Worksheet
Dim wksht5 As Worksheet
Set wksht = ThisWorkbook.Worksheets("Coils same day remove & insert")
Set wksht1 = ThisWorkbook.Worksheets("Implants same day remove&insert")
Set wksht2 = ThisWorkbook.Worksheets("Implant inserted NO Removal")
Set wksht3 = ThisWorkbook.Worksheets("Implant inserted AND removed")
Set wksht4 = ThisWorkbook.Worksheets("Coil inserted NO removal")
Set wksht5 = ThisWorkbook.Worksheets("Coil inserted AND removed")
The code below is a part of the macro that is working
wksht.Activate
With wksht
i = Range("A" & Cells.Rows.Count).End(xlUp).Row
Do Until i = 1
If .Cells(i, 1) <> "" Then
Cells(i, 9).Formula = "=IF(A" & i & "=A" & i + 1 & ",IF(C" & i & "=C" & i + 1 & ",(H" & i & "-C" & i & "),(F" & i + 1 & "-C" & i & ")),IF(A" & i & "=A" & i - 1 & ",IF(C" & i & "=C" & i - 1 & ",(H" & i & "-C" & i & "),(H" & i & "-C" & i & ")),(H" & i & "-C" & i & ")))"
End If
i = i - 1
Loop
End With
And the code below here is the part that is not working
wksht3.Activate
With wksht3
i = Range("A" & Cells.Rows.Count).End(xlUp).Row
Do Until i = 1
If .Cells(i, 1) <> "" And .Cells(i, 3) <> "" And .Cells(i, 6) <> "" Then
Cells(i, 9).Formula = "=F" & i & "-C" & i & ")"
Else: Cells(i, 9).Value = "0"
End If
i = i - 1
Loop
End With
When I debug the code it highlights the Cells(i, 9).Formula = "=F" & i & "-C" & i & ")" line
Thanks for your time
=F10-C10)
is not a valid formula so you get a 1004
The error you get is because VBA does not understand "=F" & i & "-C" & i & ")". As far as it is a string, the easiest way to debug is to write either:
debug.print "=F" & i & "-C" & i & ")" on the line above and to see the immediate window for the value
or
MsgBox "=F" & i & "-C" & i & ")" on the line above and to see the string in a MsgBox.
Based on the result you would know how to continue.
Start with putting a period in front of every Range and Cells within a With ... End With.
Brackets come in pairs.
Don't turn real numbers into text-that-looks-like-a-number.
wksht3.Activate '<~~ totally unnecessary to use a With ... End With
With wksht3
i = .Range("A" & .Cells.Rows.Count).End(xlUp).Row
Do Until i = 1
If .Cells(i, 1) <> "" And .Cells(i, 3) <> "" And .Cells(i, 6) <> "" Then
.Cells(i, 9).Formula = "=F" & i & "-C" & i
Else
.Cells(i, 9).Value = 0
End If
i = i - 1
Loop
End With
FWIW, you could also just have your formula do the tests too:
With wksht3
i = Range("A" & Cells.Rows.Count).End(xlUp).Row
.Range("I1:I" & i).FormulaR1C1 = "=IF(OR(RC1="""",RC3="""",RC6=""""),0,RC6-RC3)"
End With
I'm trying to flag problematic cells and link the cell hyperlink to another cell to review later. Here is my code. Not all of the code is visible. I Called "i" and "j" as long. The error occurs on newLink = Range("AL" & i).Hyperlinks(1).Address claiming that it is "out of range." I think this means that it is calling something that doesn't exist, but to be honest i'm not sure.
If Range("AK" & i).Value = "On" Then
If Range("AL" & i).Value = 0 And Range("AM" & i).Value = 0 Then
Range("AL" & i, "AM" & i).Interior.ColorIndex = 6
'Cells("AL" & i) = H.Address'
ErrorCount = ErrorCount + 1
Dim newLink As String
newLink = Range("AL" & i).Hyperlinks(1).Address
Range("IV" & j).Hyperlinks.Add anchor:=Range("IV" & j), Address:=Range("IV" & j)
Range("IV" & j).Hyperlinks(1).Address = newLink
j = j + 1
End If
If there is no hyperlink attached to a cell then Range("foo").Hyperlinks.Count will return 0 and therefore you will get an 'Out of range' error.
You just need to wrap the newLink = ... statement in an If to check if there is already a hyperlink there. E.g.
If Range("AL" & i).Hyperlinks.Count = 1 Then
newLink = Range("AL" & i).Hyperlinks(1).Address
Else
'what else will you do?
End If
What I have this far is below.
Range("F4").Value = Range("D4").Value & " x " & Range("E4")
This is creating a concatenate in Column F for me. This is working fine for the individual cell but I would like this to continue concatenating down until it hits an empty cell.
I have tried looping without success.
below is an image of the sheet
Set Concat= Rows(4)
concatstring =""
For i = 1 To Concat.Cells.Count
If Concat.Cells(i).Text <> "" Then
Concatstring = concatstring & " x " & Concat.Cells(i).Text
Else
Exit for
End If
Next
Concat.cells(I).text = concatstring
This should do it. Let me know if you still have problem.
Try this...
i=4
Do while(Range("D" & i).Value<> "")
Range("F" & i).Value = Range("D" & i).Value & " x " & Range("E" & i)
Loop
i'm trying to execute these code in my excel sheet
ActiveCell.Offset(0, 3).Formula = "=if(SUM(N" & i + 2 & ":N" & i + 5 & ")>0;MEDIAN(N" & i + 2 & ":N" & i + 5 & ");0)"
and i'm get an #1004 error with no more informations. Can anybody eyplain my failure?
I hav some others formulars insert in the same way...thx
EDIT:
My Tables look like that
This should be a projectmanagement tool - Breitband Delphi Method ;)
So my code goes through all the rows and check in which column the descripton is (level 1,2,3,4).
Next the code is adding the rows 8-12 for example.. here i can enter some informations for the project... and now my script should add the formula at column k-n.
My code is not very nice (as my english :) ) - it is just a prototype..
This is my Loop
i = 5
canSkip = False
Do
' fist first the level
If Not IsEmpty(Range("B" & i).Value) Then
level = 1
If Not IsEmpty(Range("D" & i + 1)) Then
' ye we can - so skip this loop
canSkip = True
End If
ElseIf Not IsEmpty(Range("D" & i).Value) Then
level = 2
If Not IsEmpty(Range("F" & i + 1)) Then
' ye we can - so skip this loop
canSkip = True
End If
ElseIf Not IsEmpty(Range("F" & i).Value) Then
level = 3
If Not IsEmpty(Range("H" & i + 1)) Then
' ye we can - so skip this loop
canSkip = True
End If
ElseIf Not IsEmpty(Range("H" & i).Value) Then
level = 4
canSkip = False
End If
If canSkip = True Then
i = i + 1
Else
' First insert some... and bang it to a group
' Insert Formula
Range("K" & i).Activate
ActiveCell.Formula = "=min(L" & i + 2 & ":L" & i + 5 & ")"
ActiveCell.Offset(0, 1).Formula = "=max(L" & i + 2 & ":L" & i + 5 & ")"
'Range("T1").FormulaLocal = insertMedianFormula
'ActiveCell.Offset(0, 3).Formula = "=WENN(SUMME(N" & i + 2 & ":N" & i + 5 & ")>0;MITTELWERT(N" & i + 2 & ":N" & i + 5 & ");0)"
Range("A" & i + 1).Activate
For x = 1 To 5
ActiveCell.EntireRow.Insert
If x = 5 Then
If level = 1 Then
ActiveCell.Offset(0, 1).Value = "Experte"
ActiveCell.Offset(0, 2).Value = "Aufw."
ActiveCell.Offset(0, 3).Value = "Bemerkung"
ElseIf level = 2 Then
ActiveCell.Offset(0, 3).Value = "Experte"
ActiveCell.Offset(0, 4).Value = "Aufw."
ActiveCell.Offset(0, 5).Value = "Bemerkung"
ElseIf level = 3 Then
ActiveCell.Offset(0, 5).Value = "Experte"
ActiveCell.Offset(0, 6).Value = "Aufw."
ActiveCell.Offset(0, 7).Value = "Bemerkung"
ElseIf level = 4 Then
ActiveCell.Offset(0, 7).Value = "Experte"
ActiveCell.Offset(0, 8).Value = "Aufw."
ActiveCell.Offset(0, 9).Value = "Bemerkung"
End If
' now just bang it to a group
ActiveCell.Resize(5, 10).Rows.Group
End If
Next x
i = i + 6
End If
' are we finshed?
If i > lastUsedRow Then
Exit Do
End If
canSkip = False
Loop
Original formula (MS standard) uses "," instead of ";"
ActiveCell.Offset(0, 3).Formula = "=IF(SUM(N" & i + 2 & ":N" & i + 5 & ")>0,MEDIAN(N" & i + 2 & ":N" & i + 5 & "),0)"
or use:
ActiveCell.Offset(0, 3).FormulaLocal = "=IF(SUM(N" & i + 2 & ":N" & i + 5 & ")>0;MEDIAN(N" & i + 2 & ":N" & i + 5 & ");0)"
Please, refer this:
Formula
FormulaLocal
[EDIT]
First of all...
IsEmpty indicates whether a variable (of variant) has been initialized. So, if you want to check if cell is empty (does not contains any value), use:
Range("B" & i)<>""
Second of all..
Your code has no context. What it means? Using ActiveCell or Range("") or Cell() depends on what workbook (and its sheet) is actually in usage!
You should use code in context:
With ThisWorkbook.Worksheets("SheetName")
.Range("A1").Offset(0,i).Formula = "='Hello Kitty'"
.Cell(2,i) = "123.45"
End With
Third of all...
Review and debug you code and start again using above tips ;)