Excel按指定间隔自动填1的代码实现及Stack Overflow表格创建咨询
1. Excel Formula to Repeat 1s at a Specified Interval
Let’s start with the Excel part. First, pick a cell to store your interval value—say cell A1 (you can type 3, 4, 2, etc., here). Then, for your columns starting from B1 (Col1) and extending to as many columns as you need, use this formula in each cell:
=IF(MOD(COLUMN()-COLUMN($B$1),$A$1)=0,1,0)
How this works:
COLUMN()-COLUMN($B$1)calculates how far each column is from your starting column (Col1, cell B1). For B1 itself, this is 0; for C1 it’s 1, D1 is 2, and so on.MOD(..., $A$1)checks if that offset is a multiple of your interval. If yes, it returns 0, making theIFstatement output 1. Otherwise, it outputs 0.- Just drag the formula across all the columns you need, and it’ll automatically update based on the interval you entered in A1. For example, inputting 4 will give you
1, 0, 0, 0, 1in the first five columns, exactly as you wanted.
If you prefer a macro-based approach (for more automation), here’s a quick VBA script:
Sub RepeatOnInterval() Dim interval As Integer interval = InputBox("Enter the interval:") Dim col As Integer For col = 1 To 10 ' Adjust the number 10 to your desired number of columns If (col - 1) Mod interval = 0 Then Cells(1, col).Value = 1 Else Cells(1, col).Value = 0 End If Next col End Sub
Run this macro, enter your interval when prompted, and it’ll fill the first row with 1s at the specified intervals.
2. Creating Tables on Stack Overflow
Stack Overflow uses standard Markdown table syntax—it’s straightforward once you get the hang of it.
Basic Syntax:
| Heading 1 | Heading 2 | Heading 3 | |-----------|-----------|-----------| | Row 1 Val | Row1 Val2 | Row1 Val3 | | Row2 Val1 | Row2 Val2 | Row2 Val3 |
Example (matching your Excel data):
| Col1 | Col2 | Col3 | Col4 | Col5 | |------|------|------|------|------| | 1 | 0 | 0 | 1 | 0 |
Alignment Options:
You can add colons to control text alignment:
- Left align:
|:---| - Center align:
|:---:| - Right align:
|---:|
Example with centered headers:
| Col1 | Col2 | Col3 | Col4 | Col5 | |:---:|:---:|:---:|:---:|:---:| | 1 | 0 | 0 | 1 | 0 |
When you post this syntax in a Stack Overflow question or answer, it’ll render as a clean, readable table.
内容的提问来源于stack exchange,提问作者Kon Hai Woh

