Syntax problem to check if boxed are filled with information - excel

i think i have a syntax problem.
I just want to check with vba if theres every field filled for next steps.
I need like 15 more cells to check
Maybe someone can help me
my code work, just the first line give error because of this "and ("C24:C22")"
For Each Zellen_auf_Inhalt_prüfen In Range("B7:J7") **and ("C24:C22")**
If Zellen_auf_Inhalt_prüfen.Value = "" Then
MsgBox "Bitte alle Zellen ausfüllen."
GoTo MacroEnde
End If
Next Zellen_auf_Inhalt_prüfen
"Zellen auf inhalt prüfen" means, to check cells if theres anything in
i google but i dont find any solution.

I think you are looking for the union statement:
For Each Zellen_auf_Inhalt_prüfen In Union(Range("B7:J7"), Range("C24:C22"))

Related

Userform textbox fills in if Vlookup finds the information related to the numbers inserted

I have an userform where people have to fill in with data. If the data already exists, when they put the information in the DocumentTitleBox, other textboxes should automatically fill in.
My code works with letters, but not with numbers.
For example, when I put "aaa", it returns the vlookup values. But if I put "123", it won't do anything, even though there is a vlookup for it.
I cannot figure it out why. This is part of my code:
Private Sub DocumentTitleBox_Change()
On Error Resume Next
Result = WorksheetFunction.VLookup(DocumentTitleBox.Value, Worksheets("example").Range("D:E"), 2, False)
FIND = WorksheetFunction.VLookup(DocumentTitleBox.Value, Worksheets("example").Range("D:E"), 1, False)
On Error GoTo 0
If FIND = DocumentTitleBox.Value Then
NameBox.Value = Result
End If
Thank you in advance!
I always use this kind of thing. Could be cleaned up and stuff but I like the flexibility and I change stuff all the time so this works for me.
Private Sub DocumentTitleBox_Change()
If IsNumeric(DocumentTitleBox.Value) Then
ReturnRow = Application.IfError(Application.Match(DocumentTitleBox.Value + 0, Worksheets("example").Columns(4), 0), "Not Found")
Find = Application.IfError(Application.Index(Worksheets("example").Columns(5), ReturnRow), "Not Present")
Else
ReturnRow = Application.IfError(Application.Match(DocumentTitleBox.Value, Worksheets("example").Columns(4), 0), "Not Found")
Find = Application.IfError(Application.Index(Worksheets("example").Columns(5), ReturnRow), "Not Present")
End If
If Not Find Like "Not Present" Then
NameBox.Value = Find
Else
NameBox.Value = ""
End If
End Sub
PS: I don´t know how to avoid the match functions odd behaviour with strings/numbers so I just go with the +0 and IsNumeric trick. One thing to note is case sensitivity, adjust that as needed, right now its not.
If DocumentTitleBox is a text box, try using DocumentTitleBox.Text instead of DocumentTitleBox.Value.

Excel VBA On Error error

I am rather new at programming, and while learning Python also started experimenting with Excel VBA. I have an issue with the last one.
I have some large Excel sheets and tried to validate that data in specific columns matches data on another sheet in certain columns as they will be supposed to relate to each other by these values (and will be connected by a third value). To make this a bit more difficult, both of these columns may contain more than one value separated by "|". So, I have split these values in a list and I try to iterate through them to make sure all these values are set correctly, the connection will work fine.
All is fine as long as all is fine :) I have however an issue where there are two values in one of those columns and only one in the other. I would like this discrepancy to be noted on a sheet and then proceed to the next item.
The way that seemed to be applicable for me is to use "On Error GoTo ErrHandler", then note error on another sheet, and then user Resume to proceed.
Here is what I came up with:
For h = 0 To UBound(Split1())
For j = 1 To GetMaxRow("SpecificSheet", A)
On Error GoTo ErrHandler:
If Sheets("SpecificSheet").Cells(j, 1).Value = Split1(h) And Sheets("SpecificSheet").Cells(j, 2).Value = Split2(h) Then
DependencyOk = DependencyOk + 1
End If
Next j
Next h
ErrProceed:
Also ErrHandler is:
ErrHandler:
Sheets("Issues").Cells(x, 1) = "IssueDescription"
GoTo ErrProceed
It stops at line 2 with Subscript out of range for Split2(h) rather than moving on to ErrHandler and then ErrProceed. I have the feeling this must be something very obvious but I am just unable to get this working, and I am not able to find other way (like a try/except) in Excel VBA.
UPDATE:
Trying to clarify things a bit. The root of the issue is, that the Split2 list is shorter than Split1 - which is an issue with the input data and I'd like to capture this. I get the Split values from cells, where the values are separated by "|" characters:
CellValue = Sheets("SomeSheet").Cells(RowNumber, ColumNumber)
CellValueSplit() = Split(CellValue, "|")
And then iterate as:
For h = 0 To UBound(Split1())
So as Split1 moves on to the for example 3rd value, Split2 throws error and script stops. The best I was able to do so far was, that I let it proceed with the loop, but as this is a rather large sheet, it will fill the same error report ca. 200k times in this case, which I'd like to avoid. So I'd prefer it to proceed from after this loop once it hits out of range error, and proceed examining the next value.
Thank you for your help so far and in advance!
You have an issue with your syntax. The proper Error statement syntax is:
On Error GoTo <string>
On Error Resume Next
On Error GoTo 0
When using On Error GoTo <string> there is no ":" at the end. The ":" doesn't come into play until you create the target location. Example:
On Error GoTo Here
'// ---- Do something ---- //
Here:
'// ---- Handle the error ---- //
If you use On Error Resume Next, then you're telling the machine to ignore errors and proceed on to the next line of code.
When you useOn Error Return To 0, VBA will reset its error handling back to default. It's a good habit when using On Error Resume Next to insert On Error Return To 0 as soon as you no longer need it. On Error Resume Next has a real potential to break your code and make it behave strangely. Not to mention debugging can be a real nightmare. Check out the VBA manual from Microsoft for a more detailed explanation.
Finally, if your question is answered, you should mark it as answered.
vba-excelvbaexcel
The short and quick version is that VBA Error Handling Routine's only handle errors in the actual code execution, they do not fire when conditions expressed by the code are not met.
In your case, you do not need any error handling at all. In most cases it is actually best to avoid On Error GoTo .... There are cases where it's inevitable, but they are rare.
Try this IF THEN ELSE block:
If Sheets("SpecificSheet").Cells(j, 1).Value = Split1(h) And Sheets("SpecificSheet").Cells(j, 2).Value = Split2(h) Then
DependencyOk = DependencyOk + 1
Else
Sheets("Issues").Cells(x, 1) = "IssueDescription"
End If
Actually I have just found the issue. It was caused by a ":" left after an If statement a few rows earlier. I still don't really understand what it did, but I suggest not to reproduce it :)

How I can move part of text in MS Word with VBA to right side?

I need to write macro thats splits string into two parts. One part of text goes to left allign in word, second to right.
I found in this topic manual steps for doing it, my question is how to do it automatically?
https://superuser.com/questions/484261/word-formatting-need-to-align-left-to-left-right-to-right-in-same-line
I tried to make two text columns in Word:
With ActiveDocument.PageSetup.TextColumns
.SetCount NumColumns:=2
End With
but I don't know how to put my parts of text into desired column. Can anyone help me?
Try going to the developer tab and clicking 'Record Macro'. Then do the splitting manually. After that click 'Stop Recording'. This will give you the commands needed to automatically split the text.
With Constuntine help I found solution - in every line that I want to split to left and right side I do following action:
With objWord
.Visible = False
.Selection.TypeParagraph
.Selection.TypeText ("ABC" & vbTab & "123")
.Selection.ParagraphFormat.TabStops.Add Position:=CentimetersToPoints(17), _
Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces
End With

VBScript - Either getting object required or type mismatch errors

I have scoured the web and this site looking for an answer on this, so I would really appreciate some help.
I'm creating a VBScript to do some modifications to a user-specified Excel spreadsheet. I have the first part of my script working fine, but the second part is driving me nuts. I need it to search the first column for a value and, if found, delete the row. Right now I'm not worrying about the deletion statement--I'm doing testing by seeing if I can get the For Each statement to run properly as well as the If Then statement. Here's the specific block of code:
For Each cell in objSheet.Columns("A:A").Cells
Set cell = objSheet.Columns("A:A").Cells
If cell.Value = "60802400040000" then
cell.font.bold = True
End If
Next
I have tried many variations of this and cannot find the right combination. Initially I was getting an "Object Required" messages, and after reading a number of posts, found that I needed to put in a Set statement for cell, which I did. Now I am getting a Mismatch Type error message.
The funny thing is, before I put in the Set statement, the code would execute, but it would throw the Object Required error when I closed the spreadsheet. After adding it, the error for the Type Mismatch pops up immediately.
Most examples I keep finding on the web are for VBA, and I try to modify them for VBS, which I don't know very well. Any assistance anyone can give me will be greatly appreciated.
You are redefining cell, cell is defined automatically in the For Each statement.
Delete this line
Set cell = objSheet.Columns("A:A").Cells
This is an example from Help, unfortunately Help doesn't have any examples that uses For Each, only For x = n to n and other means. For Each is the right thing to do.
Set r = Range("myRange")
For n = 1 To r.Rows.Count
If r.Cells(n, 1) = r.Cells(n + 1, 1) Then
MsgBox "Duplicate data in " & r.Cells(n + 1, 1).Address
End If
Next n
For vba to vbs, you have to create the object and use, as some objects are automatically available in VBA (like app object) - Set exceldoc = CreateObject("c:\blah\blah.xls) then to use Set r = exceldoc.worksheets(0).range("MyRange").
Also you have to use constant values not names as vbscript can't look them up.

Handling errors

I am trying to perform some operations over some data, but they are not working and I need to find a way to ignore those cells which don´t meet all the requirements.
Basically, I have a column where some cells have text + numbers in their content and other have only text. I search inside all of them and split TEXT in one column and NUMBER in another one. Then, I run a macro to find the matching text to each one in other column.
But when I try to split TEXT from NUMBERS, I search for the first "(", cause my number format is (10.10.10), but if it is not found, the cell value appears: #VALUE! (ok, it is expected cause the character was not found). Here is the problem: if I run my macro over "#VALUE! it crashs and don´t finish its execution.
I have tried to use
On Error GoTo
in my macro code, but for some reason it doesn´t not handle the "Run time error: Type Mismatch".
For contadorOr = 2 To colO
For contadorDes = 2 To colA
On Error GoTo cont
If InStr(1, Cells(contadorDes, colunaDestino).Value, Cells(contadorOr, colunaOrigem).Value) Then
If InStr(1, Cells(contadorOr, colunaOrigem + 4).Value, Cells(contadorDes, colunaDestino + 1).Value) Then
Cells(contadorOr, colunaOrigem + 5).Value = "Mesma versão"
End If
Exit For
End If
Next contadorDes
cont: Next contadorOr
Any suggestions? I can think of ignoring this error (when it´s happen, my variable contadorOr is incremented and go to no next value) or any way to avoid #VALUE! returned by my functions, but haven´t had success doing that.
Thanks in advance.
Instead of using error handling options you could check if cell which you are going to check/process doesn't return error. This is quite simple as presented below:
If IsError(Cells(contadorDes, colunaDestino).Value) Then
'to do anything if there is error
'usually...do nothing
Else
'do what you want if there is no error
End if

Resources