Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Excel VBA Newbies - Is there a better syntax to code the if

Hi:

I am new to VBA, I have the following code, in the If statement, how
can I code it better instead of using lots of "OR". Is there a better
way or string comparison.

================================================== ====================
Dim lcCol As String
lnColCount = ActiveSheet.UsedRange.Columns.Count 'Column Count
For j = 1 To lnColCount
lcCol = GetExcelColumn(j)

If lcCol = "G" Or lcCol = "H" Or lcCol = "AB" Or lcCol =
"AF" Then
Columns(lcCol + ":" + lcCol).Select
Selection.EntireColumn.Hidden = True
End If
Next j

================================================== ====================
Thank you
Peter
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Excel VBA Newbies - Is there a better syntax to code the if

Maybe:

select case lcCol
case is = "G","H","AB","AF"
'do your stuff
case is = "anymore here?"
'do other stuff
case else
'do even more different stuff
end select

But maybe even easier would be to use the number of the column:

Dim lcCol As Long
lnColCount = ActiveSheet.UsedRange.Columns.Count 'Column Count
For j = 1 To lnColCount
select case lcCol
case is 7, 8, 27, 32
columns(lccol).hidden = true
end select
next j

or maybe even:

range("G1,H1,AB1,AF1").entirecolumn.hidden = true

And be careful:

ActiveSheet.UsedRange.Columns.Count

could be 3 if your used range were AA1:AC9.

I like this:

dim LastRow as long
dim LastCol as long
with worksheets("sheet1") 'whatever
LastCol = .UsedRange.Columns(.UsedRange.Columns.Count).Colum n
LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
end with

If row 1 and column A is used (anywhere), then it's slight overkill. But every
once in awhile....


Peter wrote:

Hi:

I am new to VBA, I have the following code, in the If statement, how
can I code it better instead of using lots of "OR". Is there a better
way or string comparison.

================================================== ====================
Dim lcCol As String
lnColCount = ActiveSheet.UsedRange.Columns.Count 'Column Count
For j = 1 To lnColCount
lcCol = GetExcelColumn(j)

If lcCol = "G" Or lcCol = "H" Or lcCol = "AB" Or lcCol =
"AF" Then
Columns(lcCol + ":" + lcCol).Select
Selection.EntireColumn.Hidden = True
End If
Next j

================================================== ====================
Thank you
Peter


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax problem with code WLMPilot Excel Discussion (Misc queries) 1 March 15th 08 07:07 PM
VBA code to sum a row: syntax needed [email protected] Excel Discussion (Misc queries) 1 July 11th 05 06:41 PM
Excel VBA Newbies Peter[_50_] Excel Programming 4 November 10th 04 10:23 PM
help with code/syntax Jay Baxter Excel Programming 0 February 26th 04 08:11 PM
not for the newbies... Tereba Excel Programming 4 January 21st 04 03:05 PM


All times are GMT +1. The time now is 02:00 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"