Ford Barton
New Member
- Joined
- Oct 30, 2009
- Messages
- 7
Hello, I have a problem with a VB routine that resets cells to zero. I am an experienced user in Excel (2007, Windows XP) but a novice in VBA. I have spent many hours researching and trying to solve my error on my own and would greatly appreciate any suggestions. I have a financial spreadsheet that is used over and over again and needs to conditionally hide unused rows and columns and clear unused cells to zero.
I have gotten my VB code to hide the unused rows and columns correctly but I am having difficulties getting the code to efficiently clear unused cells to zero.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com
ffice
ffice" /><o
> </o
>
Here is the code that I have gotten to work so far:
<o
> </o
>
Sub HUColumns()
BeginColumn = 4
EndColumn = 42
ChkRow = 1
With Worksheets("HISTORICAL FINANCIAL")
For ColCnt = BeginColumn To EndColumn
If .Cells(ChkRow, ColCnt).Value = 1 Then
.Cells(ChkRow, ColCnt).EntireColumn.Hidden = True
Else
.Cells(ChkRow, ColCnt).EntireColumn.Hidden = False
End If
Next ColCnt
BeginRow = 15
EndRow = 40
ChkCol = 1
<o
> </o
>
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 1 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
End With
End Sub
<o
> </o
>
1) I have to zero out 7 columns and tried in error adding something like this:
Range(RowCnt, 4).ClearContents
Range(RowCnt, 9).ClearContents
Range(RowCnt, 14).ClearContents
Range(RowCnt, 19).ClearContents
Range(RowCnt, 25).ClearContents
Range(RowCnt, 29).ClearContents
Range(RowCnt, 34).ClearContents
2) Since the 7 columns are all spaced apart by 4, I also tried adding:
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 1 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Cells(RowCnt, ChkCol + 4).Select<o
></o
>
Selection.ClearContents
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
End With
<o
> </o
>
I know this is not right and if it did work, I would still need to reset ChkCol back to 1 before advancing to the next condition.
<o
> </o
>
3) My objective is to hide the columns, rows, and clear the data on one worksheet, and then only hide the rows, and clear the data on two other worksheets.
<o
> </o
>
Any help would be greatly appreciated. Thank you very much !
I have gotten my VB code to hide the unused rows and columns correctly but I am having difficulties getting the code to efficiently clear unused cells to zero.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com




Here is the code that I have gotten to work so far:
<o


Sub HUColumns()
BeginColumn = 4
EndColumn = 42
ChkRow = 1
With Worksheets("HISTORICAL FINANCIAL")
For ColCnt = BeginColumn To EndColumn
If .Cells(ChkRow, ColCnt).Value = 1 Then
.Cells(ChkRow, ColCnt).EntireColumn.Hidden = True
Else
.Cells(ChkRow, ColCnt).EntireColumn.Hidden = False
End If
Next ColCnt
BeginRow = 15
EndRow = 40
ChkCol = 1
<o


For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 1 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
End With
End Sub
<o


1) I have to zero out 7 columns and tried in error adding something like this:
Range(RowCnt, 4).ClearContents
Range(RowCnt, 9).ClearContents
Range(RowCnt, 14).ClearContents
Range(RowCnt, 19).ClearContents
Range(RowCnt, 25).ClearContents
Range(RowCnt, 29).ClearContents
Range(RowCnt, 34).ClearContents
2) Since the 7 columns are all spaced apart by 4, I also tried adding:
For RowCnt = BeginRow To EndRow
If Cells(RowCnt, ChkCol).Value = 1 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Cells(RowCnt, ChkCol + 4).Select<o


Selection.ClearContents
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
End With
<o


I know this is not right and if it did work, I would still need to reset ChkCol back to 1 before advancing to the next condition.
<o


3) My objective is to hide the columns, rows, and clear the data on one worksheet, and then only hide the rows, and clear the data on two other worksheets.
<o


Any help would be greatly appreciated. Thank you very much !