How to Loop Range number in Excel VBA without changing letter - vba

How do I get this to work?
I have this code written so far:
Sub RemoveLoop()
Dim i As Long
For i = 6 To 15
If Range("B" + i) = "YES" Then
Range("C" + i + ":" + "P" + i).ClearContents
End If
Next i
End Sub
Instead of doing each individually like this:
This is what I'm trying to shorten/accomplish, below:
Sub Remove()
If Range("B6") = "YES" Then
Range("C6:P6").ClearContents
End If
If Range("B7") = "YES" Then
Range("C7:P7").ClearContents
End If
If Range("B8") = "YES" Then
Range("C8:P8").ClearContents
End If
If Range("B9") = "YES" Then
Range("C9:P9").ClearContents
End If
If Range("B10") = "YES" Then
Range("C10:P10").ClearContents
End If
If Range("B11") = "YES" Then
Range("C11:P11").ClearContents
End If
If Range("B12") = "YES" Then
Range("C12:P12").ClearContents
End If
If Range("B13") = "YES" Then
Range("C13:P13").ClearContents
End If
If Range("B14") = "YES" Then
Range("C14:P14").ClearContents
End If
If Range("B15") = "YES" Then
Range("C15:P15").ClearContents
End If
End Sub
Simple question for you guys, thank you for your help.
I don't know what else to say, it's pretty straight forwards I believe. But I'm still getting the, "Looks like your most is mostly code error."
This should be an easy one for you VBA experts to solve.
Thanks again.

Try this:
Sub RemoveLoop()
Dim i As Long
Set WSheet = Worksheets("Sheet1") ' This enables the change in the mentioned sheet and not the Active sheet.
For i = 6 To 15
If WSheet.Range("B" & i) = "YES" Then
WSheet.Range("C" & i & ":P" & i).ClearContents
End If
Next i
End Sub

This is what you can do, passing the cells and the ranges as variables:
Option Explicit
Sub RemoveLoop()
Dim i As Long
For i = 6 To 15
With Worksheets(1)
If UCase(.Range("B" & i)) = "YES" Then
.Range(.Cells(i, "C"), .Cells(i, "P")).ClearContents
End If
End With
Next i
End Sub
Except for using Range(Cells,Cells), the code is refering to UCase, which makes sure that "yes" and "YES" in column "B" are treaten equally.

Related

Search and Copy Excel Data with VBA

I'm writing a VBA program to search through a large spreadsheet and copy rows that have the same account five or more times associated with the data to a different sheet. The program does exactly what it's supposed to do when I step through each individual line (F8), but when I run the program (F5), it doesn't end up copying any information to the second sheet. I've tried adding a two second delay between switching sheets and pasting the data, just in case this was the problem, but so far it hasn't helped.
Any suggestions?
Edit: I thought that the screen updating might be causing the problem, so I disabled it. The program still didn't paste the data in the other worksheet.
Second Edit: I noticed that when I put a stop in at the beginning of the while loop and step the program through in chunks, it also does not copy and paste the data like it should be. It still works when stepping through individual lines of code, though. I also removed the 2 second pauses as those didn't make a difference.
Here's the code:
Public Sub Main()
Worksheets(2).Activate
Range("A1").Select
Worksheets(1).Activate
Range("C2").Select
AcctName = ActiveCell.Value
LoopControl = 0
AcctNameCt = 1
CurrentAcctRow = ActiveCell.Row
Do While LoopControl <> 1
SecondLoopControl = 0
If AcctName = ActiveCell.Offset(AcctNameCt, 0).Value Then
AcctNameCt = AcctNameCt + 1
If AcctNameCt > 4 Then
GreaterThanFour
End If
ElseIf ActiveCell.Offset(AcctNameCt, 0).Value = "" Then
Exit Do
Else
ActiveCell.Offset(AcctNameCt, 0).Activate
AcctName = ActiveCell.Value
AcctNameCt = 1
CurrentAcctRow = ActiveCell.Row
End If
Loop
End Sub
Public Sub CopyData()
Dim EndRow As Integer
Dim StopCopy As Integer
Dim RestartRow As Integer
EndRow = CurrentAcctRow + AcctNameCt
StopCopy = EndRow - 1
RestartRow = EndRow + 1
ActiveSheet.Range("C" & CurrentAcctRow & ":" & "C" & StopCopy).EntireRow.Copy
Worksheets(2).Activate
LookForEmptyRow
ActiveCell.EntireRow.PasteSpecial
CurrentAcctRow = CurentAcctRow + 1
Worksheets(1).Activate
Range("C" & EndRow).Select
AcctNameCt = 0
End Sub
Public Sub GreaterThanFour()
Do While SecondLoopControl <> 1
If AcctName = ActiveCell.Offset(AcctNameCt, 0).Value Then
AcctNameCt = AcctNameCt + 1
Else
CopyData
SecondLoopControl = 1
End If
Loop
End Sub
Public Sub LookForEmptyRow()
Range("A1").Select
Dim LookAnotherLoopControl As Integer
LookAnotherLoopControl = 0
Do While LookAnotherLoopControl <> 1
If ActiveCell.Value = "" Then Exit Sub Else ActiveCell.Offset(1, 0).Activate
Loop
End Sub
I set the worksheet names to variables and called those, rather than calling the worksheets directly. For some reason, this works better.
Set wbA = Workbooks(Workbook Name)
Set wsA = Worksheets(Worksheet Name 1)
Set wsB = Worksheets(Worksheet Name 2)
Where the "Workbook Name" and "Worksheet Name 1" reflect the actual names, instead. Those are working better than:
Worksheets(2).Activate
LookForEmptyRow
ActiveCell.EntireRow.PasteSpecial
CurrentAcctRow = CurentAcctRow + 1
Worksheets(1).Activate
Range("C" & EndRow).Select
I also used a better method to look for an empty row, rather than writing my own subroutine. The original code had this sub that I wrote:
Public Sub LookForEmptyRow()
Range("A1").Select
Dim LookAnotherLoopControl As Integer
LookAnotherLoopControl = 0
Do While LookAnotherLoopControl <> 1
If ActiveCell.Value = "" Then Exit Sub Else ActiveCell.Offset(1, 0).Activate
Loop
Which, while effective, was highly inefficient. I replaced it with the much more efficient line of code:
lRow = Range("A1000").End(xlUp).Row
Cells(lRow + 1, 1).Activate

Input box value insert in the next empty cell in row to the right

I am trying to add to the next empty cells to the right, the data from the user form text box, if data already exists. Meaning if "E1" is has date, add to "F1" and so on, but only is the range "E1:S1".
Here is a screenshot of the report:
And here is what I've got so far (but it stops as E1):
Private Sub CommandButton1_Click()
If Range("E1") = "" Then Range("E1") = UserForm2.TextBox1.Value Else
Range("E1").End(xlToRight) = UserForm2.TextBox1.Value
If Range("E2") = "" Then Range("E2") = UserForm2.TextBox2.Value Else
Range("E2").End(xlToRight) = UserForm2.TextBox2.Value
End Sub
The End(xlToRight is only going to the end of the populated cells not the next open one. You need to move one more column over after finding the last populated cell. Use Cells() and I prefere staring at the furthest column and coming back.
Private Sub CommandButton1_Click()
If Range("E1").Value = "" Then Range("E1").Value = UserForm2.TextBox1.Value Else
Cells(1, Cells(1, Columns.Count).End(xlToLeft).Column + 1).Value = UserForm2.TextBox1.Value
If Range("E2").Value = "" Then Range("E2").Value = UserForm2.TextBox2.Value Else
Cells(2, Cells(2, Columns.Count).End(xlToLeft).Column + 1).Value = UserForm2.TextBox2.Value
End Sub

Unable to run the 2 sets of codes in one sheet

I need help for VBA as I'm new to this programming language. Is it possible to have 2 different sets of codes in one sheet in the workbook?
I want to make the Excel sheet more interactive like clicking on certain cell then highlighting the entire row that the cell is selected. But the sheet that im trying to make it interactive has a set of codes already.
Here is the codes that I want to make the excel sheet interactive
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
initializeWorksheets
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
If IsEmpty(Target) Or Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
With ActiveCell
' Highlight the row and column that contain the active cell, within the current region
Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Interior.ColorIndex = 6
End With
Next ws
Application.ScreenUpdating = True
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'filtering
Dim ws As Worksheet
ws.Activate
Dim ccolumn As Integer
Dim vvalue As String
ccolumn = ActiveCell.Column
vvalue = ActiveCell.Value
For Each ws In Worksheets
If IsEmpty(Target) Or Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
With ActiveCell
Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).AutoFilter Field:=ccolumn, Criteria1:=vvalue
Cancel = True
End With
Next ws
End Sub
Here is the codes that it is used for the same sheet:
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
initializeWorksheets
Application.ScreenUpdating = False
If (ActiveSheet.Name = "Student Viewer") Then
searchKey = Trim(Target.Range.Value)
If (Right(searchKey, 1) = ")") Then
searchKey = Right(searchKey, Len(searchKey) - InStrRev(searchKey, "(", -1))
searchKey = Left(searchKey, Len(searchKey) - 1)
End If
temp = 2
Do While (mainSheet.Range(findColumn(mainSheet, "IC Number") & temp) <> searchKey & "")
temp = temp + 1
If (temp > 65535) Then
MsgBox ("Error in Finding xxxx Details")
End
End If
Loop
viewerSheet.Unprotect
' Set details
For i = 2 To 10
viewerSheet.Range("C" & i) = mainSheet.Range(findColumn(mainSheet, Left(viewerSheet.Range("B" & i), Len(viewerSheet.Range("B" & i)) - 1)) & temp)
viewerSheet.Range("F" & i) = mainSheet.Range(findColumn(mainSheet, Left(viewerSheet.Range("E" & i), Len(viewerSheet.Range("E" & i)) - 1)) & temp)
Next i
For i = 2 To 3
viewerSheet.Range("I" & i) = mainSheet.Range(findColumn(mainSheet, Left(viewerSheet.Range("H" & i), Len(viewerSheet.Range("H" & i)) - 1)) & temp)
Next i
loadSummary
viewerSheet.Protect
ElseIf (ActiveSheet.Name = "xxxx Viewer") Then
searchKey = Trim(Target.Range.Value)
viewerSheet2.Unprotect
' Set details
temp = 2
Do While (DetailsSheet.Range(findColumn(DetailsSheet, "Policy Num") & temp) <> searchKey & "")
temp = temp + 1
If (temp > 65535) Then
MsgBox ("Error in Finding Details")
End
End If
Loop
For i = 2 To 11
viewerSheet2.Range("C" & i) = DetailsSheet.Range(findColumn(DetailsSheet, Left(viewerSheet2.Range("B" & i), Len(viewerSheet2.Range("B" & i)) - 1)) & temp)
Next i
For i = 2 To 6
viewerSheet2.Range("I" & i) = ValuesSheet.Range(findColumn(ValuesSheet, Left(viewerSheet2.Range("H" & i), Len(viewerSheet2.Range("H" & i)) - 1)) & temp)
Next i
For i = 7 To 12
viewerSheet2.Range("I" & i) = DetailsSheet.Range(findColumn(DetailsSheet, Left(viewerSheet2.Range("H" & i), Len(viewerSheet2.Range("H" & i)) - 1)) & temp)
Next i
viewerSheet2.Hyperlinks.Add Anchor:=Range("C2"), Address:="", SubAddress:="'Client Viewer'!A1"
loadDetail
viewerSheet2.Protect
End If
Application.ScreenUpdating = True
End Sub
As commented, you can try this approach:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo halt
Application.EnableEvents = False
With Me ' Me refers to the worksheet where you put this code
.Cells.Interior.ColorIndex = -4142 ' xlNone
If Not CBool(-Target.Hyperlinks.Count) Then ' Check if there is hyperlink
Target.EntireRow.Interior.ColorIndex = 6 ' or you can use RGB(255, 255, 0)
Else
Target.Hyperlinks(1).Follow ' follow hyperlink if there is
CodeFromYourFollowHyperlinkEvent ' call a routine
End If
End With
moveon:
Application.EnableEvents = True
Exit Sub
halt:
MsgBox Err.Description
Resume moveon
End Sub
As you can see above, CodeFromYourFollowHyperlinkEvent should be a sub that contains what you want done in your FollowHyperlink event as shown below.
Private Sub CodeFromYourFollowHyperlinkEvent()
' Put your code in FollowHyperlink here
initializeWorksheets
Application.ScreenUpdating = False
If (ActiveSheet.Name = "Student Viewer") Then
.
.
.
End Sub
Now take note that you need to exercise explicitly working on your objects.
To know more about that, check this cool post out.

Using .Hidden or .SpecialCells(xlCellTypeVisible) to Ignore Hidden Rows -- Not Working

This code uses .Hidden and is intended to ignore hidden rows, but the output fails to do so:
For j = [ESDataRow1].Row To lastEIRPSummaryRow
If [ESPriPred100].Rows(j).EntireRow.Hidden = False Then
If [ESPriPred100].Rows(j) >= [ESPRiSpec100].Rows(j) Then
y2count = y2count + 1
End If
End If
Next j
So I tried using .SpecialCells(xlCellTypeVisible), which produces "Application-defined or Object-defined error":
Dim cel As Range, rng As Range
Set rng = Range(Cells([ESDataRow1].Row, 1), Cells(lastEIRPSummaryRow, 1))
For Each cel In rng.SpecialCells(xlCellTypeVisible)
If [ESPriPred100].Rows(j) >= [ESPRiSpec100].Rows(j) Then
y2count = y2count + 1
End If
Next
What is wrong with my code?
Sub jzz()
Dim i As Long
For i = 1 To 5
If Rows(i).EntireRow.Hidden = True Then
Debug.Print "row: " & i & " is hidden"
Else
Debug.Print "row: " & i & " is not hidden"
End If
Next i
End Sub
Works for me, and I haven't been able to break it.
Can you test with such a simplefied sub? If that works, add 'complexity' step by step and see where it breaks.
If it doesn't work, test in a clean (new) workbook.

I cannot make this code work, it is a "type mismatch error"

Is there anything wrong with this code. I need to look through a column and if the value is less than a reference value (it is a timer) than copy adjacent cell and put in "A8".
Thanks.
Sub GetData()
Dim i As Integer
For i = 4 To 31
If Cells(i, 38) < Cells(32, 5) Then
Cells(1, 8) = Cells(i, 39)
End If
Next i
End Sub
Or alternatively to all presented options add this additional If statement before your existing if:
If IsError(Cells(i, 39)) = False And IsError(Cells(32, 5))= False Then
you could try testing for a numeric value in the cell before testing for less than:
Sub GetData()
Dim i As Integer
For i = 4 To 31
if isnumeric(cells(i,38)) then
If Cells(i, 38) < Cells(32, 5) Then
Cells(1, 8) = Cells(i, 39)
' Exit For ' UN-COMMENT to exit loop
End If
else
msgbox "Cell '" & cells(i,38).address & "' may have an error",vbexclamation+vbokonly
end if
Next
End Sub
BTW, the comments above by David and Kazjaw are quite right, every iteration of the loop you would potentionally overwrite cell A8!
You could exit the loop as soon as the test returns true like this:
Exit for
To avoid the mismatch, try comparing to the cell's text:
If Cells(i,38).Text < Cells(32,5)...