vba output only one cell

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. Windows
Hi All.
VBA Code:
Sub ab()

n = 5    '..................................size
px = n  '..................................left print control
py = n  '..................................right print control

      For i = 1 To n
      
            For j = 1 To n * 2
            
                  If j = px Or j = py Then
                           Cells(i, j) = "+"
                           Else
                           Cells(i, j) = ""
                           px = px - 1
                           py = py + 1
                  End If
            
            Next j
      Next i

End Sub
and this is the display
1618784379293.png

totally wrong
the output must be
1618784647963.png

Please,
Your input matters to me.
Thank you for reading this.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How about this?

Book1
ABCDEFGHIJKLMNOPQRS
1+
2++
3++
4++
5++
6++
7++
8++
9++
10++
Sheet1


VBA Code:
Sub ab()

n = 10    '..................................size
px = n  '..................................left print control
py = n  '..................................right print control

For i = 1 To n
    For j = 1 To n * 2
        If j = n - (i - 1) Or j = n + (i - 1) Then Cells(i, j) = "+" Else Cells(i, j) = ""
    Next j
Next i

End Sub
 
Upvote 0
Solution
Another way to do it without looping.

VBA Code:
Sub noLoop()
Dim n As Integer:       n = 5
Dim r As Range:         Set r = Range("A1").Resize(n, n * 2 - 1)

With r
    .Formula = "=IF(OR(COLUMN()=" & n & "-(ROW()-1),COLUMN()=" & n & "+(ROW()-1)),""+"","""")"
    .HorizontalAlignment = xlCenter
    .Value = .Value
    With .Borders
        .LineStyle = xlContinuous
        .Color = vbBlack
        .Weight = 2
    End With
    .ColumnWidth = 5
    .FormatConditions.Add xlCellValue, xlEqual, "=""+"""
    .FormatConditions(1).Interior.Color = vbYellow
End With

End Sub
 
Upvote 0
lrobbo314 Thank you, Work perfect.
I already click as solution plus a like.
You are good.
 
Upvote 0
Irobbo314 thanks also for your second code.

I am very, very interesting on the pre-logic and logic before you write the loop
I don't know IF when you use "with_____end with" statement you need some
kind of logic boards like this, (just one among others)
1618787178894.png

IF so, please let me know where to find.
this is my deeply concern - logic, -
Thanks.
 
Upvote 0
Hi, according to px, py logic starting on a blank worksheet :​
VBA Code:
Sub Demo1()
        N% = 5
    For R% = 1 To N
        Union(Cells(R, N + 1 - R), Cells(R, N - 1 + R)).Value2 = "¤"
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,217,263
Messages
6,135,532
Members
449,946
Latest member
Axdby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top