Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Laaibah Malik   on Feb 28 In MS Office Category.

  
Question Answered By: Addison Campbell   on Feb 28

: But can I add the part  you wrote to the same macro?

You can do anything you wish. The question is: will it do what
you want it to do?

: I tried it like this:
:
: ActiveSheet.Range("A2:ad13", Range("A2:ad13").End(xlToRight)).Copy _
: Destination:=Worksheets("DMH").Range("A2")
: Dim rngCell As Range
:
: With Worksheets("DMH")
:
: For Each rngCell In .Range("d2:d15")
: 'Copy to January
: If rngCell.Value = 1 Then _
: .Range("A2:S13", Range("A2:S13").End(xlToRight)).Copy _
: Destination:=.Range("A18")

[snip]
: 'Copy to December
: If rngCell.Value = 12 Then _
: .Range("A2:S13", Range("A2:S13").End(xlToRight)).Copy _
: Destination:=.Range("A183")

You are thinking like a typist. Look for patterns. Think about
doing more with less. Avoid typing the same thing repeatedly. This
is untested, but ...

With Worksheets("DMH")

For Each rngCell In .Range("d2:d15")

If Int(rngCell.Value) = rngCell.Value _
And rngCell.Value < 13 _
And rngCell.Value > 0 Then _
.Range("A2:S13", .Range("A2:S13").End(xlToRight)).Copy _
Destination:=.Range("A" & rngCell.Value * 15)

Next rngCell

End With

The first test (Int(rngCell.Value) = rngCell.Value) makes
certain we have an integer. The second test (rngCell.Value < 13)
makes certain the integer is not greater than 12. The third test
makes certain that the integer is not less than 1. If all tests
test true, the copy  happens.


: But get an error:
: On this..

You should have included the text of the error. Most languages
have hundreds of error messages. I do not know which one you
received and without your workbook I probably cannot reproduce the
error.



: .Range("A2:S13", Range("A2:S13").End(xlToRight)).Copy _
: Destination:=.Range("A123")
: I'm assuming it's because I'm on the ALLDATA sheet.
: Becuase if I rum it from DMH sheet  it work fine.
: I assume I have to name the sheet it's going to?

It's that second range. I think it is missing the leading
period. Try this.

With Worksheets("DMH")

.Range("A2:S13", .Range("A2:S13").End(xlToRight)).Copy _
Destination:=.Range("A123")


Which is short for this.

Worksheets("DMH").Range("A2:S13", _
Worksheets("DMH").Range("A2:S13").End(xlToRight)).Copy _
Destination:=Worksheets("DMH").Range("A123")

Share: 

 

This Question has 6 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on If statement in a macro ? Or get search suggestion and latest updates.


Tagged: