Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Set up a macro for Excel formatting

Posted on 1999-07-27
5
Medium Priority
?
219 Views
Last Modified: 2010-04-30
I need to be able to format some cells in Excel based on what number is typed in another cell.  Here's my example:
If the number in Cell C3 is a 1, then I need it to shade cells Z9:AP9.  I also need the macro to copy Z9:AP9 and Paste Special, Values only, into the same range, Z9:AP9.  If Cell C3 is a 2, then I need it to shade cells Z11:AP11 and do the paste special thing.  If Cell C3 is a 3, then I need it to shade cells Z13:AP13 and do the Paste Special thing.  This goes on through #14 for Cell C3 and skips every other row in columns Z:AP, if that makes sense.  I don't know Visual Basic, but Microsoft told me it wouldn't be that hard to do in VBA.  So if anyone can give me some help, it would be greatly appreciated.  THANKS.
0
Comment
Question by:esu4236
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
ture earned 600 total points
ID: 1527781
esu4236,

Here is some VBA code that does what you asked for.

Sub ShadeAndPasteSpecial()
  'Declare variables
  Dim v 'Will hold the value from cell C3
  Dim r 'Will hold the row to work with
  'Get numerical value from cell C3 into variable v
  v = Val(ActiveSheet.Range("C3").Value)
  'If the value is not ok, then exit
  If v < 1 Or v > 14 Or v <> Int(v) Then Exit Sub
  'Calculate row
  r = v * 2 + 7
  'Do this with the range from the cell from the cell at
  'row r, 26th column and 17 steps to the right
  With Cells(r, 26).Resize(1, 17)
    'Set grey color
    .Interior.ColorIndex = 15
    'Copy and paste as values only
    .Copy
    .PasteSpecial xlValues
  End With
  'Turn off the post-copy blinking
  Application.CutCopyMode = False
End Sub

Ture Magnusson
Karlstad, Sweden
0
 
LVL 22

Expert Comment

by:ture
ID: 1527782
Esu4236,

This code does the same as the one above, but it is a little shorter and cleaner.

Sub HelpEsuAgain()
  'Declare variables
  Dim v 'Will hold the value from cell C3
  Dim r 'Will hold the row to work with
  'Get numerical value from cell C3 into variable v
  v = Val(ActiveSheet.Range("C3").Value)
  'If the value is not ok, then exit
  If v < 1 Or v > 14 Or v <> Int(v) Then Exit Sub
  'Calculate row
  r = v * 2 + 7
  'Do this with the range from the cell from the cell at
  'row r, 26th column and 17 steps to the right
  With Cells(r, 26).Resize(1, 17)
    'Set grey color
    .Interior.ColorIndex = 15
    'Replace the cell contents with the current cell values
    .Formula = .Value
  End With
End Sub

/Ture
0
 

Author Comment

by:esu4236
ID: 1527783
THANKS A TON!!!! THIS WORKED GREAT.
0
 
LVL 22

Expert Comment

by:ture
ID: 1527784
You are welcome, esu4236. I'm glad that I could help!
And thanks for the points.

/Ture
0
 

Author Comment

by:esu4236
ID: 1527785
No problem.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question