如何在电子表格中通过编程实现被标红的字母与其它没有被标红的字母随机上下左右移动,注意标红的字母不会因为重新计算而消失,即红颜色要跟着字母一起移动。
首先,获取单元格的使用范围
maxRow=Excel.WorkSheet.UserRange.Row.Count
maxCoL=Excel.WorkSheet.UserRange.Column.Count
其次,用两个(分别代表行和列)不大于使用范围(maxRow,maxCoL)的随机数,取得一个随机单元格。
RandRow=rand()*maxRow
RandCol=rand()*maxCoL
先判断是否边缘:RandRow=1或者=maxRow
RandRow=1与下面单元格做交换,
RandRow=maxRow与上面单元格做交换,
1>RandRow>maxRow用一个随机奇偶数判断与上还是下交换
取得当前单元格
VarObjectCell1=Excel.WorkSheet.Cells(RandRow, RandCol)
如果和下面做交换
RandRow=RandRow+1
如果和上面做交换
RandRow=RandRow-1
获取要交换的单元格
VarObjectCell2=Excel.WorkSheet.Cells(RandRow, RandCol)
获取字体颜色
VarIndexColor1=VarObjectCell1.Font.ColorIndex
VarIndexColor2=VarObjectCell2.Font.ColorIndex
获取值
VarValue1=VarObjectCell1.value
VarValue2=VarObjectCell2.value
交换
VarObjectCell2.value=VarValue1
VarObjectCell2.Font.ColorIndex =VarIndexColor1
VarObjectCell1.value=VarValue2
VarObjectCell1.Font.ColorIndex =VarIndexColor2
望采纳
没看懂
这是什么电子表格呢?是自己编程写的吗?那得在你的代码上做修改。
就是在EXCEL电子表格中设定一个区域,然后在这个区域内输入了这些字母,有些标上了红色,希望所有的字母能够随机分布,但是红色要随着字母移动,而不是固定在上述单元格中。拜托啦
excel是不是要通过vb来实现
仅供参考
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <random>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> a;
int k = 0;
cout << "Before:\n";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a.push_back(k++);
cout << setw(2) << a.back() << ' ';
}
cout << '\n';
}
random_device rd;
mt19937 g(rd());
shuffle(a.begin(), a.end(), g);
cout << "After:\n";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cout << setw(2) << a[i * n + j] << ' ';
cout << '\n';
}
return 0;
}
5
Before:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
After:
20 4 22 9 18
19 14 16 23 5
1 12 10 0 3
7 24 17 21 8
2 13 11 15 6
可以把现有的每个单元格字母内容重新赋值,并在赋值中加入颜色,随机移动后再把赋值重新返回字母,这样就可以实现颜色跟随。