end if without block if, though if statements are closed - excel

I'm getting a mysterious 'End If without block If' error in my vba 2013 code. Any insight on what's going wrong?
I get the error with both 'End If's. If I comment them out, it throws a 424 error at me on the second line of either of the 'If' blocks.
Code (in ThisWorkbook):
Sub TryAgain()
Dim ran As Range
Dim strt As Range
Dim X As Long
Application.ScreenUpdating = False
Set strt = Sheet3.Range("A2")
Set ran = Sheet3.Range(strt, strt.End(xlDown))
n = 1
For Each blah In ran:
cause = blah.Offset(0, 11)
X = 0
c = 0
Do Until X = n:
citem = Sheet4.Cells(1, 1).Offset(X, 0)
If cause.Value = citem.Value Then:
citem.Offset(0, 1).Value = citem.Offset(0, 1).Value + 1
c = c + 1
End If
X = X + 1
Loop
If c = 0 Then:
bernerner = Sheet4.Cells(1, 1).Offset(X, 0)
bernerner.Offset(1, 0).Value = cause.Value
bernerner.Offset(1, 1).Value = bernerner.Offset(1, 1).Value + 1
n = n + 1
End If
Next blah
Application.ScreenUpdating = True
End Sub

I don't think you need : in VBA.
For , While and If none of these need a : at the end.

Related

VBA - Run-time error 1004 (application-defined or objecy-defined error)

The code is not working in this way, but if i use ('Wks_So.Range("A9:A150").ClearContents), instead of (Wks_So.Range("A9", Range("A9").End(xlDown)).ClearContents), the code has no problem at all.
My aim for the selection is to clear all the contents from A9 to the last cell of column A with values
Dim i As Integer
Dim j As Integer
Dim x As Integer
Application.Calculation = xlCalculationManual
Set Wks_Sb = Worksheets("Scarico_Bond")
Set Wks_So = Worksheets("Scarico_Other")
Set Wks_I = Worksheets("Invio")
Wks_Sb.Range("A9", Range("A9").End(xlDown)).ClearContents
'Wks_Sb.Range("A9:A150").ClearContents
Wks_So.Range("A9", Range("A9").End(xlDown)).ClearContents
here is the error
'Wks_So.Range("A9:A50").ClearContents
Wks_Sb.Range("D9:D140").Interior.Color = vbWhite
j = 9
k = 9
x = 8
For i = 7 To 150
If InStr(1, (Wks_I.Cells(i, 4).Value), "Obbligazioni") > 0 Then
Wks_Sb.Cells(j, 1) = x - 7
j = j + 1
Else
If InStr(1, (Wks_I.Cells(i, 4).Value), "Fondi/ETF") > 0 Then
Wks_So.Cells(k, 1) = x - 7
k = k + 1
End If
End If
x = x + 1
Next i
Application.Calculation = xlCalculationAutomatic
End Sub
I would do like this and also define some variables:
Sub yourSub()
Dim i, j, k, x As Integer
Dim lastRow_Sb, lastRow_So, lastRow_I as Integer
Dim wb As Workbook
Dim Wks_Sb, Wks_So, Wks_I As Worksheet
Set wb = ActiveWorkbook
Set Wks_Sb = wb.Worksheets("Scarico_Bond")
Set Wks_So = wb.Worksheets("Scarico_Other")
Set Wks_I = wb.Worksheets("Invio")
lastRow_Sb = Wks_Sb.Cells.SpecialCells(xlLastCell).Row
lastRow_So = Wks_So.Cells.SpecialCells(xlLastCell).Row
lastRow_I = Wks_I.Cells.SpecialCells(xlLastCell).Row
Application.ScreenUpdating = False
Wks_Sb.Range("A9:A" & lastRow_Sb).ClearContents
Wks_So.Range("A9:A" & lastRow_So).ClearContents
Wks_Sb.Range("D9:D140").Interior.Color = vbWhite
j = 9
k = 9
x = 8
For i = 7 To lastRow_I 'Or should this always be 150?
If InStr(1, (Wks_I.Cells(i, 4).Value), "Obbligazioni") > 0 Then
Wks_Sb.Cells(j, 1) = x - 7
j = j + 1
Else
If InStr(1, (Wks_I.Cells(i, 4).Value), "Fondi/ETF") > 0 Then
Wks_So.Cells(k, 1) = x - 7
k = k + 1
End If
End If
x = x + 1
Next i
Application.ScreenUpdating = True
End Sub
You should use End(xlUp) instead of twice End(xlDown):
Change
Wks_Sb.Range("A9", Range("A9").End(xlDown)).ClearContents
'Wks_Sb.Range("A9:A150").ClearContents
Wks_So.Range("A9", Range("A9").End(xlDown)).ClearContents
to:
Wks_Sb.Range("A9", Wks_Sb.Range("A1000000").End(xlUp)).ClearContents

How to make a cell reference function

Sub Nowe
X = 1
For i = 1 to 5
If Cells (i, X).Value = "What" Then
Cells (i+1, 6).Interior.ColorIndex = 6
End If
Next i
End Sub
I am showing an error on this snippet: "If Cells (i, X).Value = "What" Then"
I would like to make a reference but as functions
When I tried to run your code, the only error I got was related to not declaring x and i variables.
Option Explicit
Sub Nowe()
Dim x As Integer
Dim i As Integer
x = 1
For i = 1 To 5
If Cells(i, x).Value = "What" Then
Cells(i + 1, 6).Interior.ColorIndex = 6
End If
Next i
End Sub

how to fix type mismatch error in this specific function?

The code is working for 3 times in Excel VBA but at the forth time it gives an error. When I put a toggle point there, the code cannot pass "Duration = Duration + drtn" and "Type MisMatch" error shows up. Is there any way to fix the code?
Function simul(t)
Dim wk As Worksheet
Set wk = Worksheets("Simulation")
Dim FoundCell As Range
Duration = 0
i = 1
char = "StartStep"
drtn = 0
While i = 1
m = WorksheetFunction.CountIf(Range("A2:A30"), char)
Set FoundCell = wk.Range("A:A").Find(char)
x = FoundCell.Row
y = FoundCell.Row
Dim myarray() As Variant
ReDim myarray(m)
For j = 0 To m - 1
myarray(j) = wk.Range("E" & x)
x = x + 1
Next
If char = "A" Then
drtn = Application.Evaluate("LOGNORM.INV(RAND(),r5,r6)")
ElseIf char = "B" Then
drtn = Application.Evaluate("ABS(NtLogisticInv(RAND(),r29,r30)")
ElseIf char = "C" Then
drtn = Application.Evaluate("r17*(-LN(1-RAND()))^(1/r18)")
Else
i = 0
drtn = 0
End If
Duration = Duration + drtn
r = Rnd()
k = 0
While r > myarray(k)
k = k + 1
Wend
char = wk.Range("B" & y + k)
Wend
wk.Range("U" & t) = Duration
End Function

VBA IF statement being skipped

I've been toying with this very simple code and am completely baffled. Below are three different variations I have tried in order to figure out the issue and none have worked.
Original Code:
Sub GetResults()
Set Result = ActiveWorkbook.Worksheets("Results")
Set UTDT = ActiveWorkbook.Worksheets("UTDT")
Set RADAR = ActiveWorkbook.Worksheets("RADAR")
Dim y As Long
Dim i As Long
Dim x As Long
For x = 2 To RADAR.UsedRange.Rows.Count
For i = 2 To UTDT.UsedRange.Rows.Count
For y = 2 To Result.UsedRange.Rows.Count
If UTDT.Cells(i, 1) = RADAR.Cells(x, 1) And UTDT.Cells(i, 25) <> RADAR.Cells(x, 2) Then
Result.Cells(y, 1) = Trim(UTDT.Cells(i, 1))
Result.Cells(y, 2) = UTDT.Cells(i, 2)
Result.Cells(y, 3) = UTDT.Cells(i, 3)
Result.Cells(y, 4) = "Update"
End If
Next y
Next i
Next x
End Sub
I then remove the and portion and added a message box to simplify it:
Sub GetResults()
Set Result = ActiveWorkbook.Worksheets("Results")
Set UTDT = ActiveWorkbook.Worksheets("UTDT")
Set RADAR = ActiveWorkbook.Worksheets("RADAR")
Dim y As Long
Dim i As Long
Dim x As Long
For x = 2 To RADAR.UsedRange.Rows.Count
For i = 2 To UTDT.UsedRange.Rows.Count
For y = 2 To Result.UsedRange.Rows.Count
If UTDT.Cells(i, 1) = RADAR.Cells(x, 1) Then
MsgBox ("It WORKED!")
End If
Next y
Next i
Next x
End Sub
Still skips the IF statement. So i decided ill change it to <> in case its skipping because invisible formatting is causing it to be false.. but still no luck:
Sub GetResults()
Set Result = ActiveWorkbook.Worksheets("Results")
Set UTDT = ActiveWorkbook.Worksheets("UTDT")
Set RADAR = ActiveWorkbook.Worksheets("RADAR")
Dim y As Long
Dim i As Long
Dim x As Long
For x = 2 To RADAR.UsedRange.Rows.Count
For i = 2 To UTDT.UsedRange.Rows.Count
For y = 2 To Result.UsedRange.Rows.Count
If UTDT.Cells(i, 1) <> RADAR.Cells(x, 1) Then
MsgBox ("It WORKED!")
End If
Next y
Next i
Next x
End Sub

Scroll GuiTableControl object to find the row with a specific value in a given column

I have created an Excel spreadsheet that extracts live data from a query in SAP GUI and paste that info back into Excel
From there, I'm opening transaction ME38 to update schedule lines based off the extracted data. I need to go to the "scheduled.." column (which I know to be "txtEKET-MENGE") and change the number shown to the "Qty Delivered" (which is a part of the extraction); however, I need to only do this on certain cells in the "Schedule..." column ("txtEKET-ETENR") [data listed on extraction as well].
When it gets to "Set grid..." it ends the function and does nothing else.
Any insight would be greatly appreciated.
Here is what I've done so far.
Set xclapp = CreateObject("Excel.Application")
Set xclwbk = ThisWorkbook
Set xclsht = xclwbk.Sheets("Sheet1")
For k = 2 To ActiveCell.SpecialCells(11).Row
For j = 1 To ActiveCell.SpecialCells(11).Column
If j = 14 Then Purch = xclsht.Cells(k, j).Value
If j = 15 Then Item = xclsht.Cells(k, j).Value
If j = 16 Then SLine = xclsht.Cells(k, j).Value
If j = 8 Then PGI = xclsht.Cells(k, j).Value
Next
myTransaction = "ME38"
Session.FindById("wnd[0]/tbar[0]/okcd").Text = "/n" & myTransaction
Session.FindById("wnd[0]").sendVKey 0
On Error Resume Next
Session.FindById("wnd[0]/usr/ctxtRM06E-EVRTN").Text = Purch
Session.FindById("wnd[0]/usr/ctxtRM06E-EVRTN").caretPosition = 10
Session.FindById("wnd[0]").sendVKey 0
Session.FindById("wnd[0]/usr/txtRM06E-EBELP").Text = Item
Session.FindById("wnd[0]/usr/txtRM06E-EBELP").caretPosition = 3
Session.FindById("wnd[0]").sendVKey 0
Session.FindById("wnd[0]/tbar[1]/btn[30]").press
Session.FindById("wnd[0]/tbar[1]/btn[2]").press
Call SelectRowOnGrid
Session.FindById("wnd[0]/mbar/menu[0]/menu[0]").Select
Session.FindById("wnd[0]").Close
On Error Resume Next
Session.FindById("wnd[1]/usr/btnSPOP-OPTION1").press
Next
End If
If Err.Number <> 0 Then
'The Excel worksheet has e.g. 3 columns of data and an error column.
xclsht.Cells(j, 21).Value = "Here is an error."
Else
xclsht.Cells(j, 21).Value = "O.K."
End If
On Error GoTo 0
The following is the Function SelectRowOnGrid.
Function SelectRowOnGrid()
Dim grid As SAPFEWSELib.GuiTableControl
Dim columnname As SAPFEWSELib.GuiTableColumn
Dim texttofind As String
Set grid = Session.FindById("wnd[0]/usr/tblSAPMM06ETC_1117/")
Set columnname = Session.FindById("wnd[0]/usr/tblSAPMM06ETC_1117/txtEKET- ETENR")
texttofind = xclsht.Cells(k, 16).Value
For k = 0 To grid.RowCount - 1
If grid.GetCellValue(k, columnname) = texttofind Then
grid.SetCurrentCell
grid.DoubleClickCurrentCell
End If
Next k
End Function
I will give you an example of how to deal with a table in SAP. In your case you use the commands for a GRID and this is not correct.
for example:
myFile = "z:\tmp\test.xlsx"
mySheet = "Test"
Set xclApp = CreateObject("Excel.Application")
Set xclwbk = xclapp.Workbooks.Open(myFile)
set xclsht = xclwbk.Sheets(mySheet)
xclApp.Visible = True
xclapp.DisplayAlerts = false
k = 1
do
set myTable = session.findById("wnd[0]/usr/ssubITEMS:SAPLFSKB:0100/tblSAPLFSKBTABLE")
if k = 1 then
'rows = myTable.RowCount
cols = myTable.Columns.Count
vRows = myTable.VisibleRowCount
for j = 0 to cols - 1
xclsht.Cells(k,j + 1).Value = myTable.columns.elementAt(j).title
next
k = k + 1
end if
for i = 0 to vRows - 1
l = 1
for j = 0 to Cols - 1
on error resume next
myVariable = trim(myTable.GetCell(i,j).Text)
if err.number <> 0 then exit for
on error goto 0
if left(right(myVariable,3),1) = "," then
myVariable = replace(myVariable, "." , "")
myVariable = replace(myVariable, "," , "")
xclsht.Cells(k,l).Value = myVariable/100
else
xclsht.Cells(k,l).Value = myVariable
end if
l = l + 1
next
if err.number <> 0 then exit for
k = k + 1
next
if err.number <> 0 then exit do
myTable.VerticalScrollbar.Position = myTable.VerticalScrollbar.Position + vRows
Loop
xclapp.ActiveWorkbook.Save
Set xclwbk = Nothing
Set xclsheet = Nothing
set xclapp = Nothing
Regards,
ScriptMan

Resources