how to insert different values in different worksheets using vba code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalskedar
    New Member
    • Sep 2009
    • 66

    how to insert different values in different worksheets using vba code

    I want to insert different values in each worksheet i.e every worksheet should 've different values.I tried the below code but i m not able to set the
    range for a particular worksheet

    For ex-For Sheet1 iwant to set the range as "A1:A4" with values as 10,20,30,40

    But don't know how to set this for different cells.

    Similarly For Sheet2 iwant to set the range as "b1:b4" with values as 100,200,300,400

    If a workbook contains 10 such sheets how do i set the code

    Attached below is my code
    Public Sub my1()
    Dim WS_Count As Integer
    Dim cs As String
    cs = ActiveSheet.Nam e
    Dim r As Range
    Dim ws As Worksheet
    Dim y As Integer
    y = 1
    Dim I As Integer
    For Each ws In ThisWorkbook.Wo rksheets
    ws.Activate
    If cs = "Sheet1" Then
    Set r = ActiveSheet.Ran ge("A1:A4")
    r.Cells.Value = "10:20:30:4 0"
    Else
    Set r = ActiveSheet.Ran ge("c1:c4")
    r.Cells.Value = "100:200:300:40 0"
    End If

    Next ws


    I m not able to get the proper result with the above code...Can anyone Plz guide me...
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Code:
    Public Sub my1()
    Dim r As Range
    Dim ws As Worksheet
    Dim intRowCtr As Integer
    
    For Each ws In ThisWorkbook.Worksheets
      ws.Activate
        If ws.Name = "Sheet1" Then
          For intRowCtr = 1 To 4
            Worksheets("Sheet1").Cells(intRowCtr, 1).Value = 10 * intRowCtr
          Next
        Else
          For intRowCtr = 1 To 4
            Worksheets(ws.Name).Cells(intRowCtr, 3).Value = 100 * intRowCtr
          Next
        End If
    Next ws
    End Sub

    Comment

    • shalskedar
      New Member
      • Sep 2009
      • 66

      #3
      Thanks alot..Working.. .But just 1 more query here...

      if i want to set the range differently for every worksheet ..i mean

      Sheet1 range(A1:A5) with values as 12,56,34----so on

      Sheet2 range(B5:B10) with values as 22,16,44----so on

      i.e choosing range randomly & also the values may differ...

      For value assigning can we've an array defined..

      Can u Plz guide me...

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        I'm sure there is an easier Method, but it simply elludes me at the moment.
        Code:
        Dim r As Range
        Dim ws As Worksheet
        Dim intRowCtr As Integer
        Dim n As Integer
        Dim varRet As Variant
        
        For Each ws In ThisWorkbook.Worksheets
          ws.Activate
            If ws.Name = "Sheet1" Then
              Set r = ActiveSheet.Range("A1:A4")
                For n = 1 To r.Rows.Count
                  varRet = Split("10,20,30,40", ",")
                    r.Cells(n, 1).Value = varRet(n - 1)
                Next n
            Else
              Set r = ActiveSheet.Range("A1:A10")
                For n = 1 To r.Rows.Count
                  varRet = Split("10,20,30,40,50,60,70,80,90,100", ",")
                    r.Cells(n, 1).Value = varRet(n - 1)
                Next n
            End If
        Next ws

        Comment

        Working...